import TableLyout from '@/components/commonPublic/tableLyout.vue'
import TableData from "@/components/commonPublic/el-table"; //主页面列表

import {
    getXmFzCompanyTreeList, //树状图
    getStatisticalList, //列表页接口
} from "@/api/request.js";

import publicForm from "@/components/QualityGridManage/fromCom.vue"; //弹框form
import tree from "@/components/BehaViorManage/tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import CustomDialog from "@/components/BehaViorManage/workReportWeek/index.vue";

// import RingEcharts1 from "@/components/public/project"; //建设项目进场材料外委情况动态统计
// import peiNum from "@/components/public/peiNum"; //建设项目进场材料外委情况动态统计
// import pieStatitic from "@/components/public/pieStatitic"; //建设项目进场材料外委情况动态统计
import formJs from "./regexDetail.js"; //新增弹框配置项

// import engPage from "@/components/PCManage/engPage"; //建设项目进场材料外委情况动态统计

export default {
    name: "workReportWeek",
    data() {
        return {
            name: "workReportWeek",
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            checked: true, //查看下级按钮
            showName: "未选择公司",
            projectId: null,
            projectName: null,
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            checkData: [], //树状图默认选中id
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            isShow: false, //是否显示弹框
            status: null, //按钮状态1新增 3编辑
            statusNum: null,
            dataFid: null, //列表详情id
            currentRow: {
                fSubunitid: ""
            }, //列表详情


            // 列表
            getList: {
                fCompanyid: "", //分子公司或建设项目id
                keyType: "", //计划类型
                fStatus: "", //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: "2", // 0-待办 1-已办 传2 表示列表页查询
                fQualitygridclass: "",
                fMonth: "", //月 默认当前
                fYear: "", //年 默认当前
            },
            fFileName: "",
            pdfUrl: "",
            dialogText: "",
            loading: false,
            companyId: null,
            currentRowId: "",
            tableData: []

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog
        // engPage

    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));

        this.projectId = this.userInfo.projectId
        this.projectName = this.userInfo.projectId
        this.getList.userId = this.userInfo.projectName
        if (JSON.parse(sessionStorage.getItem("BehaViorManage_getList"))) {
            var dataList = JSON.parse(sessionStorage.getItem("BehaViorManage_getList"));
            this.getList = {
                fCompanyid: dataList.fCompanyid, // 施工标段id，不为空
                keyType: dataList.keyType, //计划类型
                fStatus: dataList.fStatus, //完成情况(待审核,执行中,已打回,已完成)
                companyid: "",
                page: 1, // 当前页数
                pageSize: 20, // 每页显示条数
                userId: "",
                modelType: 2,
                fQualitygridclass: dataList.fQualitygridclass,
                fYear: dataList.fYear,
                fMonth: dataList.fMonth
            };
        }
        if (JSON.parse(sessionStorage.getItem("BehaViorManage_treeCheck"))) {
            var data = JSON.parse(sessionStorage.getItem("BehaViorManage_treeCheck"));
            this.checkData = [data.fCompanyid];
        }
        this.getTree();
        this.btnRole(this.name)
    },
    methods: {
        refreshList() { //刷新列表
            this.yj_searchForm(this.getList);
        },
        //查看下级
        see() {
            if (this.checked == true) {
                this.getList.sublevel = 1;
            } else {
                this.getList.sublevel = 0;
            }
            this.getDataList(this.getList);
        },
        closeDialog() {
            this.isShow = false;

        },
        // 左侧树结构
        getTree() {
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getXmFzCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);

                        if (this.treeData.length==1&&this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("BehaViorManage_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名

            this.getList.fCompanyid = data.fCompanyid;

            this.getList.page = 1;
            this.getList.pageSize = 20;



            this.yj_searchForm(this.getList);
        },
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = [{
                    fEncode: "yj_view",
                    fFullname: "查看",
                    fOrder: 1
                }]
            }
        },
        getBtnName(name, status) {
            // this[name](status)
            console.log(name, status);
            if (this.currentRow != null) {
                this.statusNum = 7
                this.isShow = true;
                this.status = [true, true]
            } else {
                this.$message.warning("请选择需要查看的数据");
            }
        },
        // 点击查看
        yj_viewForm(row, label) {
            this.currentRow = { fSubunitid: "" }
            if (label == "实际提交次数") {
                this.statusNum = 7
                // this.currentRow = row
                this.isShow = true;
                this.status = [true]
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val;
            this.dataFid = val.fId;
            var status = this.currentRow //当前数据状态
            if (status != null && status.fStatuss == '编辑中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[2]['disabled'] = false //编辑
                this.btnObj[3]['disabled'] = false //删除
                this.btnObj[4]['disabled'] = false //提交

            } else if (status != null && status.fStatuss == '待审核') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[5]['disabled'] = false //审核

            } else if (status != null && status.fStatuss == '审核未通过') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[3]['disabled'] = false //删除

            } else if (status != null && status.fStatuss == '执行中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[1]['disabled'] = false //完成情况
                this.btnObj[4]['disabled'] = false //提交

            } else if (status != null && status.fStatuss == '审核中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[5]['disabled'] = false //审核
            } else if (status != null && status.fStatuss == '已打回') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[1]['disabled'] = false //完成情况
                this.btnObj[4]['disabled'] = false //提交



            }
        },
        // 获取列表数据
        yj_searchForm(data) {
            this.loading = true;
            var that = this;
            window.sessionStorage.setItem(
                "BehaViorManage_getList",
                JSON.stringify(data)
            );
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.userId

            }
            
            getStatisticalList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
    },
};